package cn.jvtc.course.management.system.dao.impl;

import cn.jvtc.course.management.system.bean.TeacherLessonDO;
import cn.jvtc.course.management.system.exception.TeacherLessonClassNexusDaoException;
import cn.jvtc.course.management.system.exception.TeacherLessonDaoException;
import cn.jvtc.course.management.system.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.List;

/**
 * 用于操作数据库的教师课程表
 *
 * @author GaoFeng2017
 * @date 2017-12-11 21:51:37
 **/

public class TeacherLessonDaoImpl {
    public boolean addTeacherLesson(TeacherLessonDO teacherLessonDO) {
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        int i = 0;
        String sql = "insert into teacher_lesson(gmt_create,name,period,campus,classroom,weekday,week_type,teacher_id,year_term_id) values(?,?,?,?,?,?,?,?,?)";

        try{
            con = JdbcUtils.getConnection();
            st = con.prepareStatement(sql);
            st.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
            st.setString(2,teacherLessonDO.getName());
            st.setString(3,teacherLessonDO.getPeriod());
            st.setByte(4,teacherLessonDO.getCampus());
            st.setString(5,teacherLessonDO.getClassroom());
            st.setByte(6,teacherLessonDO.getWeekday());
            st.setByte(7,teacherLessonDO.getWeekType());
            st.setLong(8,teacherLessonDO.getTeacherId());
            st.setLong(9,teacherLessonDO.getYearTermId());
            i = st.executeUpdate();
        } catch(Exception e) {
            throw new TeacherLessonDaoException(e);
        } finally {
            JdbcUtils.releaseResource(con,st,rs);
        }

        if(i <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 根据课程信息获取课程id
     * @description:
     * @author: GaoFeng2017
     * @date: 2017/12/13 16:52
     * @param: [teacherLessonDO]
     * @return: long
     *
     */
    public long getId(TeacherLessonDO teacherLessonDO) {

        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        String sql = "select id from teacher_lesson where name = ? and period = ? and campus = ? and classroom = ? and weekday = ? and week_type = ? and teacher_id = ?";
        long id = 0;
        try{
            con = JdbcUtils.getConnection();
            st = con.prepareStatement(sql);
            st.setString(1,teacherLessonDO.getName());
            st.setString(2,teacherLessonDO.getPeriod());
            st.setByte(3,teacherLessonDO.getCampus());
            st.setString(4,teacherLessonDO.getClassroom());
            st.setByte(5,teacherLessonDO.getWeekday());
            st.setByte(6,teacherLessonDO.getWeekType());
            st.setLong(7,teacherLessonDO.getTeacherId());
            rs = st.executeQuery();
            if(rs.next()) {
                id = rs.getLong("id");
            }
        } catch(Exception e) {
            throw new TeacherLessonClassNexusDaoException(e);
        } finally {
            JdbcUtils.releaseResource(con,st,rs);
        }
        return id;
    }

}
